package main

import (
	"context"
	"log"

	pb "grpc/proto"

	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {

	r := gin.Default()
	conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewGreeterClient(conn)

	r.GET("/ping", func(c *gin.Context) {

		// ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		// defer cancel()
		res, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: "world"})
		log.Printf("Greeting: %s", res.GetMessage())
		c.JSON(200, res.GetMessage())
	})
	r.Run()

}
